package com.ey12.db.dao;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ey12.db.domain.GmCserver;

/**
 * Data access object (DAO) for domain model class GmCserver.
 * 
 * @see com.ey12.db.domain.GmCserver
 * @author MyEclipse Persistence Tools
 */

public class GmCserverDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(GmCserverDAO.class);
	// property constants
	public static final String CSERVER_NAME = "cserverName";
	public static final String SEVER_TYPE = "severType";
	public static final String UNIT = "unit";
	public static final String CAMP = "camp";

	protected void initDao() {
		// do nothing
	}

	public void saveOrUpdate(GmCserver transientInstance) {
		log.debug("saving GmCserver instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(GmCserver persistentInstance) {
		log.debug("deleting GmCserver instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public GmCserver findById(java.lang.Integer id) {
		log.debug("getting GmCserver instance with id: " + id);
		try {
			GmCserver instance = (GmCserver) getHibernateTemplate().get(
					"com.ey12.db.domain.GmCserver", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(GmCserver instance) {
		log.debug("finding GmCserver instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding GmCserver instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from GmCserver as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCserverName(Object cserverName) {
		return findByProperty(CSERVER_NAME, cserverName);
	}

	public List findBySeverType(Object severType) {
		return findByProperty(SEVER_TYPE, severType);
	}

	public List findByUnit(Object unit) {
		return findByProperty(UNIT, unit);
	}

	public List findByCamp(Object camp) {
		return findByProperty(CAMP, camp);
	}

	public List findAll() {
		log.debug("finding all GmCserver instances");
		try {
			String queryString = "from GmCserver";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findUnit(GmCserver gmCserver,String gameName) {
		try {
			String queryString = " from GmCserver g where g.severType='"+gmCserver.getSeverType()+"' and g.cserverName='"+gmCserver.getCserverName()
			+"' and g.unit='"+gmCserver.getUnit()+"' and g.camp='"+gmCserver.getCamp()+"' and g.gmGame.gameName='"+gameName+"'";
			System.out.println(queryString);
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}
	public List defineUnit(String gameName,String cserverName,String serverType,String unit,String camp) {
		try {
			String queryString = " from GmCserver g where g.severType='"+serverType+"' and g.cserverName='"+cserverName
			+"' and g.unit='"+unit+"' and g.camp='"+camp+"' and g.gmGame.gameName='"+gameName+"'";
			System.out.println(queryString);
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}
	
	
	public List findSecondCbxById(Integer id) {
		try {
			String queryString = "select distinct cserverName from GmCserver where id=?";
			return getHibernateTemplate().find(queryString,id);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findThirdCbxByCname(String cserverName) {
		try {
			String queryString = "select distinct severType from GmCserver where cserverName=?";
			return getHibernateTemplate().find(queryString,cserverName);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public List findLastsCbxByStype(String severType) {
		try {
			String queryString = "select distinct unit from GmCserver where severType=?";
			return getHibernateTemplate().find(queryString,severType);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findSellViewSecondCbx(String gameName){
		try {
			String queryString = "from GmCserver g where g.gmGame.gameName=?";
			return getHibernateTemplate().find(queryString,gameName);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
//	public List findGameDetialData(String gameName,String cserverName,String serverType) {
//		try {
//			String queryString = "from GmCserver g where 1=1";
//			if(gameName!=null){		
//					queryString=queryString+" and g.gmGame.gameName='"+gameName+"'";									
//			}
//			if(cserverName!=null){
//					queryString=queryString+" and g.cserverName='"+cserverName+"'";								
//			}
//			if(serverType!=null){
//					queryString=queryString+" and g.severType='"+serverType+"'";								
//			}
//			System.out.println("findFundDetial--queryString="+queryString);
//			return getHibernateTemplate().find(queryString);
//		} catch (RuntimeException re) {
//			re.printStackTrace();
//			log.error("find all failed", re);
//			throw re;
//		}
//	}

	public GmCserver merge(GmCserver detachedInstance) {
		log.debug("merging GmCserver instance");
		try {
			GmCserver result = (GmCserver) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(GmCserver instance) {
		log.debug("attaching dirty GmCserver instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(GmCserver instance) {
		log.debug("attaching clean GmCserver instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static GmCserverDAO getFromApplicationContext(ApplicationContext ctx) {
		return (GmCserverDAO) ctx.getBean("GmCserverDAO");
	}
}